package com.example.firstboot.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.firstboot.entity.AppPatients;
import com.example.firstboot.entity.TbUser;
import com.example.firstboot.service.AppPatientsService;
import com.example.firstboot.service.TbDocondutyService;
import com.example.firstboot.service.TbUserService;
import com.example.firstboot.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private TbUserService tbUserService;
    @Autowired
    private TbDocondutyService tbDocondutyService;
    @Autowired
    private AppPatientsService appPatientsService;
    @RequestMapping("/queryDoc")
    @ResponseBody
    public JsonResult queryDoc(@RequestBody JSONObject jsonObject){
        System.out.println(jsonObject);
        String department= String.valueOf(jsonObject.get("department"));
        List<TbUser> list=tbUserService.selectByrole(2,department);
        if (list.size()==0){
            return new JsonResult(-1,"请求失败，没有查询到门诊医生信息！");
        }else {
            return new JsonResult(list,"请求成功");
        }
    }

    @RequestMapping ("/timeChoose")
    @ResponseBody
    public JsonResult timeChoose(@RequestBody JSONObject jsonObject){
//        用户选择的时间段
        String time=String.valueOf(jsonObject.get("timechoose"));
        String account=String.valueOf(jsonObject.get("account"));
        int num=0;
        switch (time){
            case "time1": num=tbDocondutyService.remainingnumByt1(account);break;
            case "time2": num=tbDocondutyService.remainingnumByt2(account);break;
            case "time3": num=tbDocondutyService.remainingnumByt3(account);break;
            case "time4": num=tbDocondutyService.remainingnumByt4(account);break;
            case "time5": num=tbDocondutyService.remainingnumByt5(account);break;
            case "time6": num=tbDocondutyService.remainingnumByt6(account);break;
        }
        JSONObject object=new JSONObject();
        object.put("num",num);
        System.out.println(object);
        if (num==0){
            return new JsonResult(-1,"该医生门诊号源已为空");
        }else{
            num--;
            switch (time){
                case "time1":  tbDocondutyService.numSourceDec1(account,num);;break;
                case "time2":  tbDocondutyService.numSourceDec2(account,num);;break;
                case "time3":  tbDocondutyService.numSourceDec3(account,num);;break;
                case "time4":  tbDocondutyService.numSourceDec4(account,num);;break;
                case "time5":  tbDocondutyService.numSourceDec5(account,num);;break;
                case "time6":  tbDocondutyService.numSourceDec6(account,num);;break;
            }
            return new JsonResult(object,"请求成功！");
        }

    }

    @RequestMapping("/appointmentPatients")
    @ResponseBody
    public JsonResult appointmentPatients(@RequestBody JSONObject jsonObject){
//        System.out.println(jsonObject);
        String paccount=String.valueOf(jsonObject.get("paccount"));
        String department=String.valueOf(jsonObject.get("department"));
        String departmenttime=String.valueOf(jsonObject.get("departmenttime"));
        String daccount=String.valueOf(jsonObject.get("daccount"));
        int num=appPatientsService.savePatients(paccount,department,departmenttime,daccount);
        if (num<1){
            return new JsonResult(-1,"请求失败，没有添加预约信息！");
        }else {
            return new JsonResult(num,"请求成功");
        }
    }

    @RequestMapping("/checkRepeat")
    @ResponseBody
    public JsonResult checkRepeat(@RequestBody JSONObject jsonObject){
        System.out.println(jsonObject);
        String paccount=String.valueOf(jsonObject.get("paccount"));
        String department=String.valueOf(jsonObject.get("department"));
        String departmenttime=String.valueOf(jsonObject.get("departmenttime"));
        String daccount=String.valueOf(jsonObject.get("daccount"));
        List<AppPatients> list=appPatientsService.checkRepeat(paccount,department,departmenttime,daccount);
        if (list.size() == 0) {
            return new JsonResult(-1, "没有重复挂号！");
        } else {
            return new JsonResult(list, "重复挂号!");
        }
    }




}
